package org.damocode.iot.network.udp.gateway;

import lombok.Getter;
import org.damocode.iot.core.device.DeviceOperator;
import org.damocode.iot.core.message.codec.EncodedMessage;
import org.damocode.iot.core.message.codec.Transport;
import org.damocode.iot.core.protocol.ProtocolSupport;
import org.damocode.iot.core.server.session.DeviceSession;
import org.damocode.iot.network.udp.UdpMessage;
import org.damocode.iot.network.udp.UdpSupport;

import java.net.InetSocketAddress;

/**
 * @Author: zzg
 * @Date: 2022/7/19 15:51
 * @Version: 1.0.0
 */
public class UnknownUdpDeviceSession implements DeviceSession {

    private UdpSupport support;

    private InetSocketAddress address;

    private long lastPingTime;

    private long connectTime = System.currentTimeMillis();

    @Getter
    private Transport transport;

    @Getter
    private final ProtocolSupport protocolSupport;

    public UnknownUdpDeviceSession(UdpSupport support,
                                   InetSocketAddress address,
                                   Transport transport,
                                   ProtocolSupport protocolSupport) {
        this.support = support;
        this.address = address;
        this.transport = transport;
        this.protocolSupport = protocolSupport;
    }

    @Override
    public DeviceOperator getOperator() {
        return null;
    }

    @Override
    public String getId() {
        return "udp-" + address.toString();
    }

    @Override
    public String getDeviceId() {
        return "known";
    }

    @Override
    public long lastPingTime() {
        return lastPingTime;
    }

    @Override
    public long connectTime() {
        return connectTime;
    }

    public Boolean send(EncodedMessage encodedMessage){
        support.publish(new UdpMessage(encodedMessage.getPayload(), address));
        return true;
    }

    @Override
    public void close() {

    }

    @Override
    public void ping() {
        lastPingTime = System.currentTimeMillis();
    }

    @Override
    public boolean isAlive() {
        return true;
    }

    @Override
    public void onClose(Runnable call) {

    }

}
